package com.scu.qingshi.domain.interview.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scu.qingshi.config.cummunity.CommunityFactory;
import com.scu.qingshi.domain.interview.dto.*;
import com.scu.qingshi.domain.interview.param.InterviewDetailRequest;
import com.scu.qingshi.domain.interview.response.AwaitingInterviewsResponse;
import com.scu.qingshi.infrastructure.dao.po.InterUserMap;
import com.scu.qingshi.infrastructure.dao.po.InterviewProcess;
import com.scu.qingshi.infrastructure.dao.po.Node;
import com.scu.qingshi.infrastructure.dao.service.InterUserMapService;
import com.scu.qingshi.infrastructure.dao.service.InterviewProcessService;
import com.scu.qingshi.infrastructure.dao.service.NodeService;
import com.scu.qingshi.infrastructure.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 面试流程查询服务
 * @author qingshi
 */
@Service
public class InterviewQueryService {

    @Autowired
    InterviewProcessService interviewProcessService;

    @Autowired
    NodeService nodeService;

    @Autowired
    InterUserMapService interUserMapService;

    public ProcessDTO getDetail(InterviewDetailRequest request){

        // 查询该用户和面试流程的映射
        QueryWrapper<InterUserMap> IUMWrapper = new QueryWrapper<>();
        IUMWrapper.eq("inter_id",request.getInterId());
        IUMWrapper.eq("user_id",request.getUserId());
        InterUserMap map = interUserMapService.getOne(IUMWrapper);

        // 查询面试流程信息
        InterviewProcess process = interviewProcessService.getById(request.getInterId());

        ProcessDTO detail = new ProcessDTO();
        detail.setInterId(request.getInterId());
        detail.setContent(process.getInterContent());
        detail.setCurNode(map.getCurNodeId());
        detail.setNodeCount(process.getNodeCount());
        detail.setIsPublish(process.publishVO());
        detail.setName(process.getName());
        detail.setCommunityName(CommunityFactory.getCommunity(process.getOpenId()).getName());
        detail.setDeadline(DateUtil.format(process.getDeadline()));
        detail.setSuccess(map.getSuccess());

        // 查询用户可查看的节点信息
        QueryWrapper<Node> nodeWrapper = new QueryWrapper<>();
        nodeWrapper.in("node_id",process.canViewNodes(detail.getCurNode()));
        List<Node> nodeList = nodeService.list(nodeWrapper);

        List<NodeDTO> dto = nodeList.stream().map(node -> {
            NodeDTO nodeDTO = new NodeDTO();
            nodeDTO.setNodeId(node.getNodeId());
            nodeDTO.setInformWay(node.getInformWay());
            nodeDTO.setInterContent(node.getInterContent());
            nodeDTO.setInterPlace(node.getInterPlace());
            nodeDTO.setIsOnline(node.onlineVO());
            nodeDTO.setStartTime(DateUtil.format(node.getStartTime()));
            nodeDTO.setEndTime(DateUtil.format(node.getEndTime()));
            return nodeDTO;
        }).collect(Collectors.toList());

        detail.setNodes(dto);

        return detail;
    }

    public CommunityProcessDTO getCommunityDetail(Integer interId) {

        InterviewProcess process = interviewProcessService.getById(interId);

        List<Integer> idList = process.getNodeList();
        List<Node> nodeList = null;
        if (idList.isEmpty()){
            nodeList = new ArrayList<>();
        }else {
            QueryWrapper<Node> nodeWrapper = new QueryWrapper<>();
            nodeWrapper.in("node_id",idList);
            nodeList = nodeService.list(nodeWrapper);
        }

        return CommunityProcessDTO.builder()
                .interId(process.getInterId())
                .content(process.getInterContent())
                .deadline(DateUtil.format(process.getDeadline()))
                .name(process.getName())
                .nodeCount(process.getNodeCount())
                .nodes(nodeList.stream().map(node ->
                        CommunityNodeDTO.builder()
                                .informFail(node.getInformFail())
                                .informPass(node.getInformPass())
                                .nodeId(node.getNodeId())
                                .interPlace(node.getInterPlace())
                                .interContent(node.getInterContent())
                                .informWay(node.getInformWay())
                                .isOnline(node.onlineVO())
                                .startTime(DateUtil.format(node.getStartTime()))
                                .endTime(DateUtil.format(node.getEndTime()))
                                .build()).collect(Collectors.toList()))
                .build();
    }

    public AwaitingInterviewsResponse getAwaitingInterview(Integer interId) {

        InterviewProcess interviewProcess = interviewProcessService.getById(interId);

        QueryWrapper<InterUserMap> wrapper = new QueryWrapper<>();
        wrapper.eq("inter_id",interId)
                .eq("pass",1);
        List<InterUserMap> maps = interUserMapService.getBaseMapper().selectList(wrapper);

        List<AwaitingInterviewDTO> dtos = maps.stream().map(map -> AwaitingInterviewDTO.builder()
                .curRound(interviewProcess.curRound(map.getCurNodeId()))
                .interName(interviewProcess.getName())
                .interId(interId)
                .review(map.getReview())
                .state(map.getSuccess())
                .userId(map.getUserId())
                .curNodeId(map.getCurNodeId())
                .build()).collect(Collectors.toList());

        AwaitingInterviewsResponse res = new AwaitingInterviewsResponse();
        res.setProcess(dtos);
        res.setNodeCount(interviewProcess.getNodeCount());
        return  res;
    }
}
